<?php

namespace app\admin\controller\mall\goods;

use app\common\controller\Backend;
use fast\Tree;
use think\Cache;


/**
 * 商品分类
 *
 * @icon fa fa-circle-o
 */
class Cat extends Backend
{
    
    /**
     * Cat模型对象
     * @var \app\common\model\mall\goods\Cat
     */
    protected $model = null;
    protected $catlist = [];
    protected $multiFields = 'ismenu,status';
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\goods\Cat;
        $this->view->assign("statusList", $this->model->getStatusList());
        // 必须将结果集转换为数组
        $catList = collection($this->model->order('weigh', 'asc')->select())->toArray();
        foreach ($catList as &$v)
        {
            $v['simplename'] = __($v['simplename']);
        }
        unset($v);
        //生成分类树
        Tree::instance()->init($catList);
        //生成权节点的字段为'simplename'
        $this->catlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'simplename');
        //生成父节点数据，用于表单选择器
        $catdata = [0 => __('None')];
        foreach ($this->catlist as &$v)
        {
            if (!$v['showis']) continue;
            $catdata[$v['id']] = $v['simplename'];
        }
        $this->view->assign('catdata', $catdata);
    }
    
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            $list = $this->catlist;
            $total = count($this->catlist);
            
            $result = array("total" => $total, "rows" => $list);
            
            return json($result);
        }
        return $this->view->fetch();
    }
    
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params)
            {
                if (!$params['showis'] && !$params['pid'])
                {
                    $this->error(__('The non-menu rule must have parent'));
                }
                $validate = new \app\common\validate\mall\goods\Cat;
                $result = $validate->check($params);
                if ($result){
                    $this->model->save($params);
                }else{
                    $this->error($this->model->getError());
                }
                Cache::rm('__goodscat__');
                $this->success();
            }
            $this->error();
        }
        return $this->view->fetch();
    }
    
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row) $this->error(__('No Results were found'));
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params)
            {
                if (!$params['showis'] && !$params['pid'])
                {
                    $this->error(__('The non-menu rule must have parent'));
                }
                //这里需要针对name做唯一验证
                $validate = new \app\common\validate\mall\goods\Cat;
                //$validate->rule([
                //    'name' => 'require|format|unique:SysUserRule,name,' . $row->id,
                //]);
                $result = $validate->check($params);
                if ($result){
                    $row->save($params);
                }else{
                    $this->error($row->getError());
                }
                Cache::rm('__goodscat__');
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    
    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $delIds = [];
            foreach (explode(',', $ids) as $v)
            {
                $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
            }
            $delIds = array_unique($delIds);
            $count = $this->model->where('id', 'in', $delIds)->delete();
            if ($count)
            {
                $this->success();
            }
        }
        $this->error();
    }

    public function getCatSelector()
    {
        //生成父节点数据，用于表单选择器
        $catdata = [0 => __('None')];
        foreach ($this->catlist as &$v)
        {
            if (!$v['showis']) continue;
            $catdata[$v['id']] = $v['simplename'];
        }
        return $catdata;
    }

    public function selectpage()
    {
        return parent::selectpage();
    }


}
